/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "WidgetBase.h"

namespace PQBuilder
{
	QVector<WidgetBase*> WidgetBase::_widgets;

	WidgetBase::WidgetBase(QWidget *parent)
		: QWidget(parent)
	{
		_widgetId=_widgets.size();
		_widgets.push_back(this);
	}

	WidgetBase::~WidgetBase()
	{
		_widgets[_widgetId]=0;
	}

	void WidgetBase::selectRenderProperty()
	{
		int cnt=_widgets.size();
		for(int i=0;i<cnt;i++){
			if(_widgets[i]){
				_widgets[i]->OnRenderPropertySelected();
			}
		}
	}


	void WidgetBase::handleEvent(PQEngine::Event* evt)
	{
		if(!evt||evt->isHandled()||!evt->sender()){
			return;
		}

		PQEngine::Object* obj=evt->sender();
		if(PQEngine::Object::isGameObject(obj)){
			PQEngine::GameObject* object=(PQEngine::GameObject*)obj;
			switch(evt->type()){
				case PQEngine::Event::EVENT_OBJECT_CREATED:
					OnGameObjectCreated(object);
					return;
				case PQEngine::Event::EVENT_OBJECT_DELETED:
					OnGameObjectDeleted(object);
					return;
				case PQEngine::Event::EVENT_OBJECT_SELECTED:
					OnGameObjectSelected(object);
					return;
				case PQEngine::Event::EVENT_OBJECT_DESELECTED:
					OnGameObjectDeselected(object);
					return;
				default:
					
					return;

			}
		}

		
		if(PQEngine::Object::isComponent(obj)){
			PQEngine::Component* component=(PQEngine::Component*)obj;
			switch(evt->type()){
				case PQEngine::Event::EVENT_OBJECT_CREATED:
					OnComponentCreated(component);
					return;
				case PQEngine::Event::EVENT_OBJECT_DELETED:
					OnComponentDeleted(component);
					return;
				case PQEngine::Event::EVENT_COMPONENT_ATTACHED:
					OnComponentAttached(component);
					return;
				case PQEngine::Event::EVENT_COMPONENT_DETACHED:
					OnComponentDetached(component);
					return;
				case PQEngine::Event::EVENT_COMPONENT_PROPERTY_UPDATED:
					OnComponentUpdated(component);
					return;
				default:
					return;
			}
		}
	}


}
